<template>
    <div class="mainDiv">
        <el-row :gutter="20" class="rowHead">
            <el-col :span="12">
                <el-button type="success" icon="el-icon-plus" @click="add" v-qx="permission.add">新增员工</el-button>
                <el-select v-model="page.zt" class="ml" @change="getList" v-qx="permission.status">
                    <el-option v-for="s in status" :label="s.txt" :value="s.val">
                    </el-option>
                </el-select>
            </el-col>
            <el-col :span="9">
                <el-input placeholder="搜索员工姓名、员工电话" @keyup.enter.native="getList" v-model="page.search">
                    <i slot="suffix" class="el-icon-search" style="line-height: 40px;width: 25px;cursor: pointer;" @click="getList"></i>
                </el-input>
            </el-col>
            <el-col :span="3">
                <el-button type="primary" icon="el-icon-h-shuaxin1" style="float:right" @click="getList">刷新列表</el-button>
            </el-col>
        </el-row>
        <el-table :data="list" stripe height="100%">
            <el-table-column prop="id" label="ID" width="80"></el-table-column>
            <el-table-column prop="name" label="员工姓名"></el-table-column>
            <el-table-column prop="shopname" label="所属分店">
                <template scope="scope">
                    {{scope.row.shopname || '直属总部'}}
                </template>
            </el-table-column>
            <el-table-column prop="bumenname" label="所在部门">
                <template scope="scope">
                    {{scope.row.bumenname || '直属总部'}}
                </template>
            </el-table-column>
            <el-table-column prop="sex" label="员工性别">
                <template scope="scope">
                    <i class="el-icon-h-nv1 color-nv" v-if="!scope.row.sex"></i>
                    <i class="el-icon-h-nan1 color-nan" v-else></i>
                </template>
            </el-table-column>
            <el-table-column prop="username" label="员工电话"></el-table-column>
            <el-table-column prop="pname" label="所属职位"></el-table-column>
            <el-table-column prop="zname" label="所属职称"></el-table-column>
            <el-table-column prop="ismanage" label="管理层">
                <template scope="scope">
                    <el-tag type="success" v-if="scope.row.ismanage"><i class="el-icon-h-shi"></i></el-tag>
                    <el-tag type="danger" v-else><i class="el-icon-h-fou"></i></el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="remarks" label="员工描述" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="addtime" label="添加日期">
                <template scope="scope">
                    {{scope.row.addtime|dateTime}}
                </template>
            </el-table-column>
            <el-table-column label="操作" width="120">
                <template scope="scope">
                    <el-dropdown size="small" trigger="click" @command="moreMenu" v-if="!scope.row.isdisable">
                        <el-button type="primary" size="small">更多操作<i class="el-icon-arrow-down el-icon--right"></i></el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item :command="{row: scope.row, tp: 1}" v-qx="permission.edit">编辑员工</el-dropdown-item>
                            <el-dropdown-item :command="{id: scope.row.id, tp: 2}" v-qx="permission.reset">重置密码</el-dropdown-item>
                            <el-dropdown-item :command="{id: scope.row.id, st: 1, tp: 3}" v-qx="permission.unstop">员工离职</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                    <el-tooltip content="复职员工" placement="left" :open-delay="800" v-else v-qx="permission.unstop">
                        <el-button type="success" icon="el-icon-h-qiyong2" size="mini" @click="stop(scope.row.id,0)"></el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <div class="block center bg-light">
            <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page="page.pages" :page-sizes="[15, 30, 50, 100]" :page-size="page.limits" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import C from '#/src/common/common.js'

export default {
    name: 'userList',
    data() {
        return {
            list: [],
            total: 0,
            page: {
                pages: 1,
                limits: 15,
                search: '',
                zt: 0
            },
            status: [{
                txt: '正常',
                val: 0
            }, {
                txt: '停职',
                val: 1
            }],
            permission: {
                add: 'userlist_add',
                status: 'userlist_status',
                edit: 'userlist_edit',
                unstop: 'userlist_unstop',
                reset: 'userlist_reset'
            },
        }
    },
    methods: {
        moreMenu: function(com) {
            if (com.tp === 1) {
                this.edit(com.row)
            } else if (com.tp === 2) {
                this.resetPassWord(com.id)
            } else if (com.tp === 3) {
                this.stop(com.id, com.st)
            }
        },

        resetPassWord: function(id) {
            C.confirm('重置当前员工帐号密码为当前手机号，是否继续？', async() => {
                let res = await C.ajax('/organization/user/resetpassword', { id: id })
                if (res) C.success(res.data)
            })
        },

        add: async function() {
            let data = await C.openBrowserWindow('/addUser', {}, { 
                width: 400,
                height: 625,
                useSpa: true,
                spaModel: 'organization',
                transparent: true,
                frame: false
            })
            if (data) {
                C.success(data.data)
                this.getList()
            }
        },

        edit: async function(row) {
            let data = await C.openBrowserWindow('/addUser', {row: row}, { 
                width: 400,
                height: 625,
                useSpa: true,
                spaModel: 'organization',
                transparent: true,
                frame: false
            })
            if (data) {
                C.success(data.data)
                this.getList()
            }
        },

        stop: async function(id, st) {
            let that = this
            if (st) {
                C.confirm('停用当前员工, 是否继续?', async() => {
                    let res = await C.ajax('/organization/user/unstop', { id: id, st: st }, 'post')
                    if (res) {
                        C.success(res.data)
                        that.getList()
                    }
                })
            } else {
                let res = await C.ajax('/organization/user/unstop', { id: id, st: st }, 'post')
                if (res) {
                    C.success(res.data)
                    that.getList()
                }
            }
        },
        sizeChange: function(val) {
            this.page.limits = val
            this.getList()
        },

        currentChange: function(val) {
            this.page.pages = val
            this.getList()
        },

        getList: async function() {
            let that = this
            let data = await C.ajax('/organization/user/userlist', this.page, 'post')
            if (data) {
                that.list = data.data
                that.total = data.count
            }
        }
    },
    mounted: function() {
        this.getList()
    }

}
</script>